﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Xml;
using System.Xml.Linq;

using Model;

namespace Controller
{
    public class XMLParser
    {
        private XDocument xDoc;

        public List<Brand> Brands { get; set; }
        public List<Category> Categories { get; set; }
        public List<SalesOrganization> SalesOrganization { get; set; }

        public XMLParser()
        {
            // CheckVersion();
        }

        public void Parse(string xmlPath, string data)
        {
            xDoc = XDocument.Load(xmlPath);

            switch (data)
            {
                case "Brands": GetBrands(); break;
                case "Categories": GetCategories(); break;
                case "Products": GetProducts(); break;
            }
        }

        public void GetBrands()
        {
            List<Brand> brands =
                (from brand in xDoc.Descendants("Brand")
                 select new Brand
                 {
                     Id = int.Parse(brand.Descendants("BrandCode").First().Value),
                     BrandName = brand.Descendants("BrandName").First().Value
                 }).ToList<Brand>();

            this.Brands = brands;
        }

        public void GetCategories()
        {
            List<Category> categories =
                (from category in xDoc.Descendants("Category")
                 select new Category
                 {
                     Id = int.Parse(category.Descendants("Id").First().Value),
                     Name = category.Descendants("Name").First().Value,
                     Description = category.Descendants("Description").First().Value
                 }).ToList<Category>();

            this.Categories = categories;
        }

        public void GetProducts()
        {
            SalesOrganization sOrganization = new SalesOrganization();
            List<SalesOrganization> sOList =
                (from _sO in xDoc.Element("ProductFeed").Elements("SalesOrganization")
                 select new SalesOrganization
                 {
                     Id = int.Parse(_sO.Element("SalesOrganizationId").Value),
                     CreatedTime = _sO.Element("CreatedTime").Value,
                     NumberOfProducts = int.Parse(_sO.Element("NumberOfProducts").Value),
                     Currency = _sO.Element("Currency").Value,
                     Products =
                        (from _p in _sO.Elements("Products").Elements("Product")
                         select new Product
                         {
                             Id = int.Parse(_p.Element("ProductNumber").Value),
                             Price = decimal.Parse(_p.Element("Price").Value),
                             BrandCode = int.Parse(_p.Element("BrandCode").Value),
                             AvailabilityLevel = _p.Element("AvailabilityLevel").Value,
                             CategoryId = int.Parse(_p.Element("Category").Value),
                             Status = _p.Element("ProductStatus").Value,
                             LaunchDate = _p.Element("LaunchDate").Value,
                             ProductDetails =
                                (from _pdetails in _p.Element("Descriptions").Elements("Description")
                                 select new ProductDetail
                                 {
                                     Language = _pdetails.Element("Language").Value,
                                     Description = _pdetails.Element("Description").Value,
                                     Notes = _pdetails.Element("ReplacementNotes").Value
                                 }).ToList()
                         }).ToList()
                 }).ToList();

            this.SalesOrganization = sOList;
        }

        /// <summary>
        /// Before parsing the XML file and uploading the data to the database, it ought to be checked
        /// </summary>
        /// <returns>String containing month-day-year</returns>
        public string CheckVersion(string xmlPath)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(xmlPath);

            XmlNodeList xnList = xml.SelectNodes("/ProductFeed/SalesOrganization");
            string[] createdTime = new string[2];

            foreach (XmlNode xn in xnList)
            {
                string version = xn["CreatedTime"].InnerText;
                createdTime = version.Split('T');
            }

            return createdTime[0];

            /*Console.WriteLine("Created at: {0}", DateTime.Parse(createdTime[0]).Ticks);
            Console.WriteLine("Test: {0}", DateTime.Parse("22-02-2013").Ticks);

            if (DateTime.Parse("22-02-2013").Ticks > DateTime.Parse(createdTime[0]).Ticks)
                Console.WriteLine("Korrekt");
            else
                Console.WriteLine("Forkert");*/
        }
    }
}